<?php

namespace app\api\controller\goods;

use app\api\controller\Common;

class Info extends Common
{
    protected $data;

    public function initialize()
    {
        parent::initialize();
        $this->data = $this->request->except(['sign', 'accessKey', 'token']);
    }

    /**
     * 品牌列表
     */
    public function brand_list()
    {
        $brand_list = db('goods_brand')->field('id,title,title_en,pic')->where('del', 0)->where('is_show', 1)->order('sort', 'desc')->select();
        if ($brand_list) {
            $this->return = ['code' => 'SUCCESS', 'message' => '获取成功', 'status' => 200, 'data' => $brand_list];
        } else {
            $this->return = ['code' => 'GET_ERROR', 'message' => '查询失败', 'status' => 200];
        }
        return $this->show($this->return);
    }

    public function brand_detail()
    {
        $id = input('id');
        $detail = db('goods_brand')->field('id,title,pic,content,create_time')->where('id', $id)->find();
        $detail['content'] = str_replace('src="', 'src="' . config('img_host'), $detail['content']);

        return $this->showSuccess('品牌详情', $detail);
    }

    /**
     * 商品分类--普通用户与加盟商展示分类不同
     */
    public function category()
    {
        $this->checkUserLogin();
        //品牌列表
        $list = db('goods_category')->field('id,title')->where(['pid' => 0, 'del' => 0, 'is_show' => 1])->order('sort', 'desc')->select();
        $user_id = $this->user['id'];
        $user_type = $this->user['user_type'];

        foreach ($list as $key => $val) {
            $brand_id = $val['id'];
            if ($user_type == 3) {
                $exist = db('goods_brand_agent')->where(['brand_id' => $brand_id, 'user_id' => $user_id, 'status' => 1])->select();
                if ($exist) {
                    $list[$key]['agent_sub'] = [
                        ['id' => 1, 'title' => '院装产品'],
                        ['id' => 2, 'title' => '客装产品'],
                    ];
                    $list[$key]['user_sub'] = [];
                } else {
                    $list[$key]['agent_sub'] = [];
                    $child = db('goods_category')->field('id,title')->where(['pid' => $val['id'], 'del' => 0, 'is_show' => 1])->order('sort', 'desc')->select();
                    if ($child) {
                        $list[$key]['user_sub'] = $child;
                    }
                }
            } else {
                $list[$key]['agent_sub'] = [];
                $child = db('goods_category')->field('id,title')->where(['pid' => $val['id'], 'del' => 0, 'is_show' => 1])->order('sort', 'desc')->select();
                if ($child) {
                    $list[$key]['user_sub'] = $child;
                }
            }
        }
        return $this->showSuccess('商品分类列表', $list);
    }


    /**
     * 商品列表
     */
    public function list()
    {
        $page = $this->data['page'] ? $this->data['page'] : 1;
        $pageSize = $this->data['limit'] ? $this->data['limit'] : 20;
        $paginate = ['list_rows' => $pageSize, 'page' => $page];
        $category_id = $this->data['category_id'];
        $agent_product_id = $this->data['agent_product_id'];
        $brand_id = $this->data['brand_id'];
        $keyword = $this->data['keyword'];
        $where[] = ['a.del', '=', 0];
        $where[] = ['a.status', '=', 1];
        $user_id = $this->user['id'];
        $user_type = $this->user['user_type'];

        if ($brand_id) {
            $where[] = ['a.brand_id', '=', $brand_id];
        }
        if ($category_id) {
            $where[] = ['a.category_one', '=', $category_id];
        }
        if ($agent_product_id) {
            $where[] = ['a.agent_product_id', '=', $agent_product_id];
        }
        if ($keyword) {
            $where[] = ['a.name', 'like', '%' . $keyword . '%'];
        }

        $field = 'a.id, a.name,a.pic,b.title brand_name,';
        if ($user_type == 3) {
            $field .= " a.sale_price";
        } elseif ($user_type == 4) {
            $field .= " a.join_price";
        } else {
            $field .= " a.agent_price";
        }
        $list = db('goods_info')->alias('a')
            ->field($field)
            ->leftJoin('goods_category b', 'b.id = a.brand_id')
            ->where($where)->order('a.sort', 'desc')->order('a.id', 'desc')->paginate($paginate)->toArray();
        // if($list){
        //     if($list['data']){
        //         foreach ($list['data'] as $key=>$val){
        //             $pic = db('goods_info_pic')->field('id,pic')->where('gid',$val['id'])->find();
        //             $list['data'][$key]['pic'] = $pic['pic'];
        //         }
        //     }
        // }
        return $this->showSuccess('商品列表', $list);
    }


    /**
     * 商品详情：加盟商价格与普通用户价格有别
     */
    public function detail()
    {
        $id = $this->data['id'];
        if (!$id) {
            $this->showError('缺少商品ID', 'GET_ERROR');
        }
        $user_id = $this->user['id'];
        $user_type = $this->user['user_type'];

        db('goods_info')->where('id', $id)->setInc('hot'); // 热度+1

        $field = 'a.id,a.name,a.sku,a.code,a.original_price,a.stock,a.status,a.desc,a.content,';
        if ($user_type == 3) {
            $field .= " a.sale_price";
        } elseif ($user_type == 4) {
            $field .= " a.join_price";
        } else {
            $field .= " a.agent_price";
        }
        $detail = db('goods_info')->alias('a')->where('a.id', $id)
            ->field($field)
            ->field('a.brand_id,b.title as brand_name')
            ->join('goods_category b', 'b.id = a.brand_id')
            ->find();
        if ($detail) {
            $detail['content'] = str_replace('src="', 'src="' . config('img_host'), $detail['content']);
            $detail['skus'] = [];
            if ($detail['sku']) { // 颜色,尺码#白色,红色-S,M
                $exp_sku = explode('#', $detail['sku']);
                $exp_one_sku = explode(',', $exp_sku[0]);
                $exp_two_sku = explode('-', $exp_sku[1]);
                foreach ($exp_two_sku as $key => $vo) {
                    $detail['skus']['sku_type'][$exp_one_sku[$key]] = explode(',', $vo);
                }
                if ($user_type == 3) {
                    $exist = db('goods_brand_agent')->where(['brand_id' => $detail['brand_id'], 'user_id' => $user_id, 'status' => 1])->select();
                    if ($exist) {
                        $sku_list = db('goods_info_sku')->field('id sku_id,desc,join_price AS price, stock')->where('gid', $id)->select();
                    } else {
                        $sku_list = db('goods_info_sku')->field('id sku_id,desc,price,stock')->where('gid', $id)->select();
                    }
                } elseif ($user_type == 4) {
                    $exist = db('goods_brand_agent')->where(['brand_id' => $detail['brand_id'], 'user_id' => $user_id, 'status' => 1])->select();
                    if ($exist) {
                        $sku_list = db('goods_info_sku')->field('id sku_id,desc,agent_price AS price, stock')->where('gid', $id)->select();
                    } else {
                        $sku_list = db('goods_info_sku')->field('id sku_id,desc,price,stock')->where('gid', $id)->select();
                    }
                } else {
                    $sku_list = db('goods_info_sku')->field('id sku_id,desc,price,stock')->where('gid', $id)->select();
                }
                $detail['skus']['sku_price'] = $sku_list;
                $stock = 0;
                foreach ($sku_list as $key => $val) {
                    $stock += $val['stock'];
                }
                $detail['stock'] = $stock;

                unset($detail['sku']);
            }
            // 商品图片
            $pic = db('goods_info_pic')->field('id,pic')->where('gid', $id)->select();
            $detail['pics'] = $pic;
        }
        return $this->showSuccess('商品详情', $detail);
    }


}